/**
 * 任务组模块控制器
 * 
 * @author zhongfuhua
 */

var jobGroupApp = angular.module("jobGroupApp", ["publicServiceApp", "jobGroupServiceApp", "clusterServiceApp", 
	"taskIdentityServiceApp","jobDetailServiceApp","jobDetailTrackServiceApp",
	"jobGroupDeathServiceApp", "ngJsTree", "ngSelect2"]);

jobGroupApp.controller("jobGroupCtrl", [
		"$scope",
		"commonService",
		"publicService",
		"jobGroupService",
		"clusterService",
		"taskIdentityService",
		"jobDetailService",
		"jobDetailTrackService",
		"jobGroupDeathService",
		"$timeout",
		"$log",
		function($scope, commonService, publicService, jobGroupService, clusterService, taskIdentityService, 
				jobDetailService, jobDetailTrackService, jobGroupDeathService, $timeout, $log) {
			
			
			// 表格
			$scope.$jobGroupTable = $('#jobGroupTable');
			
			// 按钮
			$scope.$jobGroupAddButton = $('#jobGroupAddButton');
			$scope.$jobGroupModButton = $('#jobGroupModButton');
			$scope.$jobGroupRemoveButton = $('#jobGroupRemoveButton');
			$scope.$jobGroupPauseButton = $('#jobGroupPauseButton');
			$scope.$jobGroupResumeButton = $('#jobGroupResumeButton');
			
			// 模态框
			$scope.$jobGroupModal = $('#jobGroupModal');
			$scope.$taskIdentityModal = $('#taskIdentityModal');
			
			// 表格选中实例对象
			$scope.selectionIds = new Array();
			$scope.selections = new Array();
			
			// 监视isSucce的状态
			$scope.action = null;
			$scope.actionTaskIdentity = null;
			$scope.isSuccess = true;
			$scope.error = null;
			
			// vo
			$scope.userName = null;
			$scope.publicVO = {};
			$scope.jobGroupVO = {};
			$scope.taskIdentityVO = {};
			$scope.$taskIdentityTableCurrent = null;
			
			ffc.util.watchResult($scope, $timeout);
			
			$scope.clusterSelectConfig = {
				minimumResultsForSearch : 1, // 是否打开搜索
				data : [],			 
				placeholder : '如无选项请新增集群'			 // 占位符
			};
			
			
			/**
			 * 
			 * 功能说明:查询集群下拉框选项
			 * 
			 */
			$scope.queryClusterSelect = function(){
				clusterService.query({}, function(result){
					if(!result.result){
						$scope.error = result.msgTitle;
						$scope.isSuccess = false;
						return false;
					}
					$scope.clusterSelectConfig.data  = new Array();
					angular.forEach(result.dataList, function(data, index, array){
						var option = {
								id : data.name,
								text : data.name
						}
						$scope.clusterSelectConfig.data.push(option);
					});
					ffc.util.angularApply($scope, $timeout);
				},function(error){
					$scope.error = error;
					$scope.isSuccess = false;
				});
			}
			
			
			/**
			 * 
			 * 功能说明:查询任务组
			 * 
			 */
			$scope.queryJobGroup = function(){
				$scope.$jobGroupTable.bootstrapTable("selectPage", 1); 
			}
			
			/**
			 * 
			 * 功能说明:查询执行端
			 * 
			 */
			$scope.queryTaskIdentity = function(){
				if($scope.$taskIdentityTableCurrent != null &&  $scope.$taskIdentityTableCurrent != undefined){
					$scope.$taskIdentityTableCurrent.bootstrapTable("selectPage", 1);  
				}
			}
			
			/**
			 * 
			 * 功能说明:新增修改任务组
			 * 
			 * 
			 */
			$scope.saveJobGroup = function(){
				if($scope.jobGroupForm.$invalid){
					new MessageDialog().error("配置填写不规范，请重新填写！！！")
					return;
				}
				
				if($scope.action=="MOD"){
					jobGroupService.update($scope.jobGroupVO, function(result){
						if(!result.result){
							$scope.error = result.msgTitle;
							$scope.isSuccess = false;
							return false;
						}
						new MessageDialog().success("修改成功!!");
						$scope.$jobGroupModal.modal("hide");
						$scope.queryJobGroup(); 
					},function(error){
						$scope.error = error;
						$scope.isSuccess = false;
					});
				}else{
					jobGroupService.add($scope.jobGroupVO, function(result){
						if(!result.result){
							$scope.error = result.msgTitle;
							$scope.isSuccess = false;
							return false;
						}
						new MessageDialog().success("添加成功!!");
						$scope.$jobGroupModal.modal("hide");
						$scope.queryJobGroup();   
					},function(error){
						$scope.error = error;
						$scope.isSuccess = false;
					});
				}
			}
			
			
			
			$scope.addTaskIdentity = function(){
				if($scope.taskIdentityForm.$invalid){
					new MessageDialog().error("配置填写不规范，请重新填写！！！")
					return;
				}
				
				if($scope.actionTaskIdentity=="MOD"){
					taskIdentityService.update($scope.taskIdentityVO, function(result){
						if(!result.result){
							$scope.error = result.msgTitle;
							$scope.isSuccess = false;
							return false;
						}
						new MessageDialog().success("修改成功!!");
						$scope.$taskIdentityModal.modal("hide");
						$scope.queryTaskIdentity(); 
					},function(error){
						$scope.error = error;
						$scope.isSuccess = false;
					});
				}else{
					taskIdentityService.add($scope.taskIdentityVO, function(result){
						if(!result.result){
							$scope.error = result.msgTitle;
							$scope.isSuccess = false;
							return false;
						}
						new MessageDialog().success("添加成功!!");
						$scope.$taskIdentityModal.modal("hide");
						$scope.queryTaskIdentity();   
					},function(error){
						$scope.error = error;
						$scope.isSuccess = false;
					});
				}
			}
			
			/**
			 * 
			 * 功能说明:删除任务组
			 * 
			 */
			$scope.deleteJobGroup = function(){
				new MessageDialog().confirm("确定删除？", function() {
					jobGroupService.removeList($scope.selections, function(result){
						if(!result.result){
							$scope.error = result.msgTitle;
							$scope.isSuccess = false;
							return false;
						}
						new MessageDialog().alert("删除成功");
						$scope.queryJobGroup(); 
					},function(error){
						$scope.error = error;
						$scope.isSuccess = false;
					});
			   });
			}
			
			$scope.deleteTaskIdentity = function(){
				new MessageDialog().confirm("确定删除？", function() {
					taskIdentityService.remove($scope.taskIdentityVO, function(result){
						if(!result.result){
							$scope.error = result.msgTitle;
							$scope.isSuccess = false;
							return false;
						}
						new MessageDialog().alert("删除成功");
						$scope.queryTaskIdentity(); 
					},function(error){
						$scope.error = error;
						$scope.isSuccess = false;
					});
			   });
			}
			
			
			
			/**
			 * 
			 * 功能说明:暂停任务组
			 * 
			 */
			$scope.pauseJobGroup = function(){
				new MessageDialog().confirm("确定暂停任务组？", function() {
					jobGroupService.pause($scope.selections, function(result){
						if(!result.result){
							$scope.error = result.msgTitle;
							$scope.isSuccess = false;
							return false;
						}
						new MessageDialog().alert("暂停成功!!");
					},function(error){
						$scope.error = error;
						$scope.isSuccess = false;
					});
			   });
			}
			
			
			/**
			 * 
			 * 功能说明:唤醒任务组
			 * 
			 */
			$scope.resumeJobGroup = function(){
				new MessageDialog().confirm("确定唤醒任务组？", function() {
					jobGroupService.resume($scope.selections, function(result){
						if(!result.result){
							$scope.error = result.msgTitle;
							$scope.isSuccess = false;
							return false;
						}
						new MessageDialog().alert("唤醒成功!!");
					},function(error){
						$scope.error = error;
						$scope.isSuccess = false;
					});
			   });
			}
			

			/**
			 * 
			 * 功能说明:查询所有菜单，父子结构展示
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月20日
			 * 
			 */
			$scope.queryCurrentUser = function(){
				publicService.queryCurrentUser({}, function(data) {
					$scope.userName = data.userName;
				}, function(error) {
					$scope.error = error;
					$scope.isSuccess = false;
				});
			}
			
			$(function() {
				
				$scope.queryClusterSelect();
				$scope.queryCurrentUser();
				
				/**
				 * 
				 * 功能说明:初始化数据源表格
				 * 
				 */
				$scope.$jobGroupTable.bootstrapTable({
					queryParams : function(params) {
						var selfParams = {
							name : $scope.jobGroupVO.queryName,
							identity : $scope.jobGroupVO.queryIdentity
						}
						return $.extend({}, params, selfParams);
					},
					ajax : function(render) {
						jobGroupService.queryPage(render.data, function(result) {
							if(!result.result){
								$scope.error = result.msgTitle;
								$scope.isSuccess = false;
								return false;
							}
							ffc.util.loadTableData(render, result);  
						},function(error){
							$scope.error = error;
							$scope.isSuccess = false;
						});
						$scope.$jobGroupRemoveButton.prop('disabled', true);
						$scope.$jobGroupModButton.prop('disabled', true);
						$scope.$jobGroupPauseButton.prop('disabled', true);
						$scope.$jobGroupResumeButton.prop('disabled', true);
					},
					onExpandRow : function(index, row, $detail){
						var taskIdentityTable = $detail.html(
								"<div class=\"table-responsive\">" +
									"<table name='taskIdentityTable'></table>" +
								"</div>"
								).find("table");
						
						var columns = [];
						
						columns.push({
							field : "name",
							title : "名称",
							width : "15%",
							align : "center",
							classes : "table-column",
							valign : "middle"
						});
						
						columns.push({
							field : "identity",
							title : "标识",
							width : "20%",
							align : "center",
							classes : "table-column",
							valign : "middle"
						});
						
						columns.push({
							field : "threadNum",
							title : "任务线程池大小",
							width : "5%",
							align : "center",
							classes : "table-column",
							valign : "middle"
						});
						
						columns.push({
							field : "batchThreadNum",
							title : "任务业务数据线程池大小",
							width : "5%",
							align : "center",
							classes : "table-column",
							valign : "middle"
						});
						
						columns.push({
							field : "threadPriorityWeight",
							title : "优先级权重",
							width : "15%",
							align : "center",
							classes : "table-column",
							valign : "middle"
						});
						
						columns.push({
							field : "batchThreadPriorityWeight",
							title : "业务数据优先级权重",
							width : "15%",
							align : "center",
							classes : "table-column",
							valign : "middle"
						});
						
						columns.push({
							field : "description",
							title : "说明",
							width : "15%",
							align : "center",
							classes : "table-column",
							valign : "middle"
						});
						
						columns.push({
							title : "操作",
							width : "10%",
							align : "center",
							classes : "table-column operation",
							valign : "middle",
							formatter : taskIdentityFormatter,
							events : taskIdentityEvents
						});
						
						
						$scope.$taskIdentityTableCurrent = taskIdentityTable;
						
						taskIdentityTable.bootstrapTable({
							idField : "id",
							sidePagination: "server",
							columns : columns,
							pagination : true,
							pageList : [5, 10, 20],
							detailView : false,
							queryParamsType : "",
							queryParams : function(params) {
								var selfParams = {
									jobGroupId : row.id,
									clusterName : row.clusterName
								};
								return $.extend({}, params, selfParams);
							},
							ajax : function(render) {
								taskIdentityService.queryPage(render.data, function(result) {
									if(!result.result){
										$scope.error = result.msgTitle;
										$scope.isSuccess = false;
										return false;
									}
									ffc.util.loadTableData(render, result);  
								},function(error){
									$scope.error = error;
									$scope.isSuccess = false;
								});
							}
						})
					}
				});
				
				/**
				 * 
				 * 功能说明:每次选择时重新获取表格中被选中对象
				 * 
				 */
				$scope.$jobGroupTable.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table',function () {
					$scope.$jobGroupModButton.prop('disabled', !$scope.$jobGroupTable.bootstrapTable('getSelections').length);
					$scope.$jobGroupRemoveButton.prop('disabled', !$scope.$jobGroupTable.bootstrapTable('getSelections').length);
					$scope.$jobGroupPauseButton.prop('disabled', !$scope.$jobGroupTable.bootstrapTable('getSelections').length);
					$scope.$jobGroupResumeButton.prop('disabled', !$scope.$jobGroupTable.bootstrapTable('getSelections').length);
					$scope.selectionIds = new Array();
					$scope.selections = new Array();
					angular.forEach($scope.$jobGroupTable.bootstrapTable('getSelections'), function(data, index, array) {
						$scope.selectionIds.push(data.id);
						$scope.selections.push(data);
					});
				})
				
								
				
				/**
				 * 
				 * 功能说明:格式化表格输出
				 * 
				 * 
				 */
				taskIdentityFormatter = function(value, row, index) {
					return "<a class=\"operation-update\"><i class=\"glyphicon glyphicon-pencil\"></i>修改</a>"+
						   "<a class=\"line\">|</a>" +
						   "<a class=\"operation-del\"><i class=\"glyphicon glyphicon-trash\"></i>删除</a>";
				}
				
				/**
				 * 
				 * 功能说明:格式化表格输出
				 * 
				 */
				jobGroupFormatter = function(value, row, index) {
					var button = "<button type=\"button\" class=\"btn btn-xs btn-blue\" style=\"margin-right: 3px;\" title=\"添加执行端配置\"><i class=\"glyphicon glyphicon-plus-sign\"></i> 执行端实例</button>"+
			         "<button type=\"button\" class=\"btn btn-xs btn-success\" style=\"margin-right: 3px;\"  title=\"运行中的子任务\">运行中</button>"+
			         "<button type=\"button\" class=\"btn btn-xs btn-warning\" style=\"margin-right: 3px;\"  title=\"待重试的子任务\">待执行</button>"+
			         "<button type=\"button\" class=\"btn btn-xs btn-danger\" style=\"margin-right: 3px;\"  title=\"已卡死的子任务\">死任务</button>";;
					return button;
				}
				
				/**
				 * 
				 * 功能说明:格式化事件
				 * 
				 */
				taskIdentityEvents = {
					'click .operation-update' : function(e, value, row, index) {
						$scope.taskIdentityVO = {};
						angular.copy(row, $scope.taskIdentityVO);
						$scope.actionTaskIdentity = "MOD";
						ffc.util.angularApply($scope, $timeout);
						$scope.$taskIdentityModal.modal("show");
					},
					'click .operation-del' : function(e, value, row, index) {
						$scope.taskIdentityVO = {};
						$scope.taskIdentityVO.clusterName = row.clusterName;
						$scope.taskIdentityVO.id = row.id;
						$scope.deleteTaskIdentity();
					}
				};
				
				
				/**
				 * 
				 * 功能说明:格式化事件
				 * 
				 */
				jobGroupEvents = {
					'click .btn-blue' : function(e, value, row, index) {
						$scope.taskIdentityVO = {};
						$scope.taskIdentityVO.jobGroupId = row.id;
						$scope.taskIdentityVO.clusterName = row.clusterName;
						$scope.actionTaskIdentity = "ADD";
						ffc.util.angularApply($scope, $timeout);
						$scope.$taskIdentityModal.modal("show");
					},
					'click .btn-success' : function(e, value, row, index) {
						$scope.publicVO.jobDetailRunningInit(row);
					},
					'click .btn-warning' : function(e, value, row, index) {
						$scope.publicVO.jobDetailRetryInit(row);
					},
					'click .btn-danger' : function(e, value, row, index) {
						$scope.publicVO.jobDetailDeathInit(row);
					}
				};
				
				/**
				 * 
				 * 功能说明:弹出框关闭事件
				 * 
				 */
				$scope.$taskIdentityModal.on("hidden.bs.modal",function(){
					$scope.action = null;
					$scope.taskIdentityForm.$setPristine();
					$scope.taskIdentityVO = {};
					ffc.util.angularApply($scope, $timeout);
				});
				
				/**
				 * 
				 * 功能说明:弹出框关闭事件
				 * 
				 */
				$scope.$jobGroupModal.on("hidden.bs.modal",function(){
					$scope.action = null;
					$scope.jobGroupForm.$setPristine();
					$scope.jobGroupVO = {};
					ffc.util.angularApply($scope, $timeout);
				});
				
				
				/**
				 * 
				 * 功能说明:弹出框打开事件
				 * 
				 */
				$scope.$jobGroupModal.on('shown.bs.modal',function(){
					if($scope.action=="MOD"){
						if($scope.selections.length > 1){
							$scope.$jobGroupModal.modal("hide");
							new MessageDialog().warning("只能选择一条记录！");
							return false;
						} 
						$scope.jobGroupVO = angular.copy($scope.selections[0]);
						ffc.util.angularApply($scope, $timeout);
					}
				});
				
			});
}]);
